﻿using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.ViewModel;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using WebServerConnectorDll;

namespace FruitManagerApp
{
    public class OfficeAddViewModel : NotificationObject
    {
        private OfficeAdd gWindow;
        private OfficeViewModel gOfficeViewModel;

        public OfficeAddViewModel(OfficeAdd pWindow, OfficeViewModel pOfficeViewModel)
        {
            gWindow = pWindow;
            gOfficeViewModel = pOfficeViewModel;

            Initialize();
        }

        public void Initialize()
        {
            HttpResponse response = API.SelectProvince();
            if(response.S == HttpResponse.Success)
            {
                Province = JSON.parse<ObservableCollection<CProvince>>(response.D);
            }
        }

        private int uid;
        public int UID
        {
            get
            {
                return uid;
            }

            set
            {
                uid = value;
                RaisePropertyChanged("UID");
            }
        }

        private String name;
        public String Name
        {
            get
            {
                return name;
            }

            set
            {
                name = value;
                RaisePropertyChanged("Name");
            }
        }

        private int provinceUID;
        public int ProvinceUID
        {
            get
            {
                return provinceUID;
            }

            set
            {
                provinceUID = value;
                RaisePropertyChanged("ProvinceUID");
            }
        }

        private ObservableCollection<CProvince> province = new ObservableCollection<CProvince>();
        public ObservableCollection<CProvince> Province
        {
            get
            {
                return province;
            }

            set
            {
                province = value;
                RaisePropertyChanged("Province");
            }
        }

        private int cityUID;
        public int CityUID
        {
            get
            {
                return cityUID;
            }

            set
            {
                cityUID = value;
                RaisePropertyChanged("CityUID");
            }
        }

        private ObservableCollection<CCity> city = new ObservableCollection<CCity>();
        public ObservableCollection<CCity> City
        {
            get
            {
                return city;
            }

            set
            {
                city = value;
                RaisePropertyChanged("City");
            }
        }

        protected DelegateCommand<object> saveCommand;
        public DelegateCommand<object> SaveCommand
        {
            get
            {
                return saveCommand ?? (saveCommand = new DelegateCommand<object>(ExcuteSaveCommand));
            }
        }

        protected virtual void ExcuteSaveCommand(object obj)
        {
            API.InsertOffice(new COffice(UID, Name, CityUID));
            gOfficeViewModel.UpdateList();
            gWindow.Close();
        }

        protected DelegateCommand<object> quitCommand;
        public DelegateCommand<object> QuitCommand
        {
            get
            {
                return quitCommand ?? (quitCommand = new DelegateCommand<object>(ExcuteQuitCommand));
            }
        }

        protected virtual void ExcuteQuitCommand(object obj)
        {
            gWindow.Close();
        }

        protected DelegateCommand<object> _provinceChanged;
        public DelegateCommand<object> _ProvinceChanged
        {
            get
            {
                return _provinceChanged ?? (_provinceChanged = new DelegateCommand<object>(ProvinceChanged));
            }
        }

        protected virtual void ProvinceChanged(object obj)
        {
            City.Clear();
            HttpResponse response = API.SelectCity(ProvinceUID);
            if (response.S == HttpResponse.Success)
            {
                City = JSON.parse<ObservableCollection<CCity>>(response.D);
            }
        }
    }
}
